package com.glb.gilibuy.service.impl;

import com.glb.gilibuy.dao.AdminDao;
import com.glb.gilibuy.entity.Admin;
import com.glb.gilibuy.entity.AdminExample;
import com.glb.gilibuy.service.AdminService;
import com.glb.gilibuy.util.MD5Util;
import com.glb.gilibuy.util.SnowflakeIdUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Description:
 * @Author: Yupeng Zhang
 * @Date: 2021-08-09 15:05
 */
@Service
public class AdminServiceImpl implements AdminService {
    @Autowired
    private AdminDao adminDao;

    @Override
    public int addAdmin(String adminName, String password) {
        //判空
        if("".equals(adminName)||"".equals(password)){
            return 0;
        }
        Admin admin = new Admin();
        //生成id
        admin.setAdminId(String.valueOf(SnowflakeIdUtil.getSnowflakeId()));
        //激活
        admin.setIsLocked(0);
        //创建日期
        admin.setGmtCreate(new Date());
        //账户名密码
        admin.setAdminName(adminName);
        admin.setPassword(MD5Util.md5Encode(password));
        if(adminDao.insertSelective(admin) > 0){
            return 1;
        }
        return 0;
    }

    @Override
    public int checkAdmin(String adminName, String password) {
        //判空
        if("".equals(adminName)||"".equals(password)){
            return 0;
        }
        AdminExample example = new AdminExample();
        example.or().andAdminNameEqualTo(adminName).andIsLockedEqualTo(0);
        List<Admin> admins = adminDao.selectByExample(example);
        if(admins.isEmpty()){
            return 0;
        }
        Admin admin = adminDao.selectByExample(example).get(0);
        if(admin.getPassword().equals(MD5Util.md5Encode(password))){
            return 1;
        }
        return 0;
    }

    @Override
    public Admin selectAdminByAdminName(String adminName) {
        AdminExample example = new AdminExample();
        example.or().andAdminNameEqualTo(adminName);
        if(adminDao.selectByExample(example).isEmpty()){
            return null;
        }
        Admin admin = adminDao.selectByExample(example).get(0);
        return admin;
    }

    @Override
    public List<Admin> findALLAdmin() {
        AdminExample example = new AdminExample();
        example.or();
        if(adminDao.selectByExample(example).isEmpty()){
            return null;
        }
        List<Admin> admins = adminDao.selectByExample(example);
        return admins;
    }


    @Override
    public int lockAdminByAdminId(String adminId) {
        AdminExample example = new AdminExample();
        example.or().andAdminIdEqualTo(adminId).andIsLockedEqualTo(0);
        Admin admin = adminDao.selectByExample(example).get(0);
        admin.setIsLocked(1);
        System.out.println(admin);
        if(adminDao.updateByExampleSelective(admin,example) > 0){
            return 1;
        }
        return 0;
    }

    @Override
    public int unlockAdminByAdminId(String adminId) {
        AdminExample example = new AdminExample();
        example.or().andAdminIdEqualTo(adminId);
        Admin admin = adminDao.selectByExample(example).get(0);
        admin.setIsLocked(0);
        if(adminDao.updateByExampleSelective(admin,example) > 0){
            return 1;
        }
        return 0;
    }


}
